package cn.likole.oj.module;


import cn.likole.oj.bean.Team;
import cn.likole.oj.bean.Teamlink;
import cn.likole.oj.bean.User;
import cn.likole.oj.bean.UserInfo;
import jdk.nashorn.internal.objects.annotations.Where;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.Callback;
import org.nutz.lang.util.IntSet;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.plugins.apidoc.annotation.Api;

import javax.naming.Name;
import javax.servlet.http.HttpSession;
import java.util.List;

@IocBean
@At("/team")
@Filters(@By(type = CheckSession.class, args = {"uid", "/"}))
@Api(name = "队伍模块")
public class TeamModule {

    @Inject
    private Dao dao;


    @At
    public Object createTeam(HttpSession session,@Param("teamname")String tname){
        NutMap re=new NutMap();
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo==null)
            return re.setv("ok",false).setv("msg","没有该江湖人士或未下山");
        if(userInfo.getTid()!=0)
            return re.setv("ok",false).setv("msg","该江湖人士已有队伍");
        Team team=new Team();
        team.setTeamName(tname);
        team.setMemNum(1);
        team.setCapId(userInfo.getId());
        team.setSolved(0);
        team.setSubmit(0);
        team=dao.insert(team);
        userInfo.setTid(team.getId());
        userInfo.setCap(true);
        int rows1=dao.update(userInfo);
        List<Teamlink> teamlinks=dao.query(Teamlink.class,Cnd.where("userId","=",userInfo.getId()));
        if(teamlinks!=null)
            dao.delete(teamlinks);
        if(rows1==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");
        return re.setv("ok",true);
    }


    @At
    public List<User> getPatList(HttpSession session){
        String sqlstr="SELECT * from user where user_id in (select id from user_info where team_id=0) and user_id!=@userId LIMIT 0,50";
        Sql sql= Sqls.create(sqlstr);
        sql.setParam("userId",session.getAttribute("uid"));
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(User.class));
        dao.execute(sql);
        return sql.getList(User.class);
    }

    @At
    @Api(name = "获取队伍信息",description = "获取人数未满的队伍信息列表,无参数")
    public List<Team> getTeamList(HttpSession session){
        return dao.query(Team.class,Cnd.where("valid","=",false));
    }



    @At
    @Api(name = "申请入队",description = "申请加入队伍，参数队伍id：teamid")
    public Object askJoin(HttpSession session,@Param("teamId")int teamId){
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo.getTid()!=0)
            return new NutMap().setv("ok",false).setv("msg","已有队伍");
        Teamlink teamlink=dao.fetchx(Teamlink.class,teamId,userInfo.getId());
        if(teamlink!=null)
            return new NutMap().setv("ok",false).setv("msg","该队伍已经邀请你");
        teamlink=new Teamlink();
        teamlink.setTeamId(teamId);
        teamlink.setUserId((int)session.getAttribute("uid"));
        teamlink.setState(false);
        dao.insert(teamlink);
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "邀请用户入队",description = "邀请用户入队,队伍teamId,用户userId")
    public Object inviteMem(@Param("teamId")int teamId,@Param("userId")int userId){
        Team team=dao.fetch(Team.class,teamId);
        if(team.getMemNum()==3)
            return new NutMap().setv("ok",false).setv("msg","队伍成员已满");
        Teamlink teamlink=dao.fetchx(Teamlink.class,teamId,userId);
        if(teamlink!=null)
            return new NutMap().setv("ok",false).setv("msg","该成员已经提交申请");
        teamlink=new Teamlink();
        teamlink.setTeamId(teamId);
        teamlink.setUserId(userId);
        teamlink.setState(true);
        dao.insert(teamlink);
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "获取队伍请求",description = "获取未处理的teamlink(请求)")
    public List<Teamlink> getReqInfo(HttpSession session){
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo.isCap())
            return dao.query(Teamlink.class,Cnd.where("teamId","=",userInfo.getTid()).and("state","=",false));
        return dao.query(Teamlink.class,Cnd.where("userId","=",session.getAttribute("uid")).and("state","=",true));
    }


    @At
    @Api(name = "同意入队",description = "同意某用户的入队申请,队伍teamId,用户userId")
    public Object acceptMem(@Param("teamId")int teamId,@Param("userId")int userId){
        NutMap re=new NutMap();
        Team team=dao.fetch(Team.class,teamId);
        if(team.getMemNum()==3)
            return re.setv("ok",false).setv("msg","人数已满");
        team.setMemNum(team.getMemNum()+1);
        int rows1=dao.update(team);
        UserInfo userInfo=dao.fetch(UserInfo.class,userId);
        userInfo.setTid(teamId);
        int rows2=dao.update(userInfo);
        Teamlink teamlink=dao.fetchx(Teamlink.class,teamId,userId);
        int rows3=dao.delete(teamlink);
        if(rows1==0||rows2==0||rows3==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");

        return re.setv("ok",true);
    }

    @At
    @Api(name = "拒绝入队",description = "拒绝用户入队申请,队伍teamId,用户userId")
    public Object rejectMem(@Param("teamId")int teamId,@Param("userId")int userId){
        Teamlink teamlink=dao.fetch(Teamlink.class,Cnd.where("teamId","=",teamId).and("userId","=",userId));
        int rows=dao.delete(teamlink);
        if(rows==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }




    @At
    @Api(name = "接受邀请",description = "接受邀请,队伍teamId")
    public Object acceptInvite(@Param("teamId")int teamId,HttpSession session){
        //check member number
        Team team=dao.fetch(Team.class,teamId);
        if(team.getMemNum()==3)
            return new NutMap().setv("ok",false);
        //update userInfo
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        userInfo.setTid(teamId);
        int rows1=dao.update(userInfo);
        //update teamInfo
        team.setMemNum(team.getMemNum()+1);
        int rows2=dao.update(team);
        Teamlink teamlink=dao.fetchx(Teamlink.class,teamId,session.getAttribute("uid"));
        dao.delete(teamlink);
        if(rows1==0||rows2==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "拒绝邀请",description = "拒绝邀请,队伍teamId,用户userId")
    public Object rejectInvite(@Param("teamId")int teamId,@Param("userId")int userId){
        Teamlink teamlink=dao.fetchx(Teamlink.class,teamId,userId);
        int rows=dao.delete(teamlink);
        if(rows==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }


    @At
    public Object dissolve(HttpSession session){
        NutMap re=new NutMap();
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        Team team=dao.fetch(Team.class,userInfo.getTid());
        int rows1=dao.delete(team);
        int rows2=dao.update(UserInfo.class, Chain.make("tid",0).add("isCap",false),Cnd.where("tid","=",team.getId()));
        if(rows1==0||rows2==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");
        return re.setv("ok",true);
    }


}
